package com.yenlien.traceyun.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.admin.interceptor.TokenProperties;
//import com.yenlien.traceyun.business.service.ResourceService;
import com.yenlien.traceyun.business.service.rest.usercenter.UserRestService;
import com.yenlien.traceyun.common.crypt.TokenUtil;
import com.yenlien.traceyun.common.exception.TokenException;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-03-06
 * @description:
 **/

@RestController
@RequestMapping(value = "user")
public class UserController extends BaseController {

    @Autowired
    private UserRestService userRestService;

    @Autowired
    private TokenProperties tokenProperties;

//    @Autowired
//    private ResourceService resourceService;

    @Value("${traceyun.app.id}")
    private String appId;

    /**
     * 登录
     *
     * @param userInfo
     * @return
     */
    @PostMapping(value = "login")
    public Object login(@RequestBody JSONObject userInfo) {
        CheckParams.checkJson(userInfo, new String[]{"username", "password"});
        JSONObject userJson = userRestService.login(userInfo.getString("username"), userInfo.getString("password"), appId);
        JSONObject result = new JSONObject();
        result.put("token", TokenUtil.getToken(userJson.toJSONString(), tokenProperties.getSecret(), tokenProperties.getExpire()));
        result.put("refresh", TokenUtil.getRefreshToken(tokenProperties.getSecret(), tokenProperties.getRefresh()));
        return result;
    }


    /**
     * 刷新token
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/refresh/token")
    public Object refresh(HttpServletRequest request) throws TokenException {
        String refresh = request.getHeader("refresh");
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(refresh)) {
            throw new TokenException("refresh is not exist");
        }
        if (StringUtils.isEmpty(token)) {
            throw new TokenException("未登录");
        }
        TokenUtil.veryRefresh(refresh, tokenProperties.getSecret());
        JSONObject result = new JSONObject();
        result.put("token", TokenUtil.getToken(TokenUtil.getBody(token), tokenProperties.getSecret(), tokenProperties.getExpire()));
        result.put("refresh", TokenUtil.getRefreshToken(tokenProperties.getSecret(), tokenProperties.getRefresh()));
        return result;
    }


//    /**
//     * 获取用户当前菜单
//     * @return
//     */
//    @GetMapping(value="/current/menus")
//    public Object menu(){
//         return resourceService.findMenusByUserId(getLoginUserId());
//    }
//
//    /**
//     * 获取应用下所有菜单
//     * @return
//     */
//     @GetMapping(value="/all/menus")
//    public Object allMenu(){
//        return resourceService.findMenus();
//    }

}
